---
title: "Exploratory Data Analysis"
output: html_document
---

```{r setup, include=FALSE, message=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```


# Univariate Plots Section
```{r}
mtcars <- within(mtcars, {
   vs <- factor(vs, labels = c("V", "S"))
   am <- factor(am, labels = c("automatic", "manual"))
   cyl  <- ordered(cyl)
   gear <- ordered(gear)
   carb <- ordered(carb)
})
dim(mtcars)
```


```{r}
str(mtcars)
```


```{r}
summary(mtcars)
```
Our dataset has 11 variables with 32 observations.


```{r, message=FALSE, warning=FALSE}
library(ggplot2)
ggplot(mtcars, aes(x = mpg)) +
    geom_histogram()
```
Most cars have a fuel consumption of between 15 to 24.

```{r}
summary(mtcars$mpg)
```



```{r, message=FALSE, warning=FALSE}
ggplot(mtcars, aes(x = disp)) +
    geom_histogram()
```
Most cars have displacement of between 100 to 170.

```{r}
summary(mtcars$disp)
```


```{r, message=FALSE, warning=FALSE}
ggplot(mtcars, aes(x = hp)) +
    geom_histogram()
```
Most cars have horsepower of below 200.

```{r}
summary(mtcars$hp)
```


```{r, message=FALSE, warning=FALSE}
ggplot(mtcars, aes(x = drat)) +
    geom_histogram()
```


```{r, message=FALSE, warning=FALSE}
ggplot(mtcars, aes(x = wt)) +
    geom_histogram()
```


```{r, message=FALSE, warning=FALSE}
ggplot(mtcars, aes(x = qsec)) +
    geom_histogram()
```


```{r}
ggplot(mtcars, aes(x = cyl)) +
    geom_bar()
```
Most cars have engines with 8 cylinders.


```{r}
ggplot(mtcars, aes(x = vs)) +
    geom_bar()
```
Most cars have engines with V shape.


```{r}
ggplot(mtcars, aes(x = am)) +
    geom_bar()
```
Most cars in the dataset have an automatic transmission.


```{r}
ggplot(mtcars, aes(x = gear)) +
    geom_bar()
```


```{r}
ggplot(mtcars, aes(x = carb)) +
    geom_bar()
```


# Univariate Analysis
## What is the structure of your dataset?
There are 32 cars in our dataset with 11 variables (mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb). Categorical variables are cyl, vs, am, gear, carb.

Other observations:
A majority of cars have 3 forward gears. 
There are also a numbers of cars with 4 forward gears.
The mean fuel consumption of the cars is 20mpg.
The median horsepower is 123.
Most cars have less than 5 carburetors.

## What is the main feature of interest in your dataset?
The main feature of interest in this dataset is mpg which is the fuel consumption of the cars. I want to know which features of car influence fuel consumption and can be used to predict the fuel consumption of a car.

## What other features in the dataset do you think will help support your investigation into your feature(s) of interest?
Cyl, vs, am, disp, hp, and gear are features that may influence fuel consumption of a car. 


# Bivariate Plots Section 
```{r}
df <- mtcars[, c(1,3,4,5,6,7)]
rs = cor(df)
round(rs, 2)
```


```{r}
library(corrplot)
corrplot(rs, type = "upper", order = "hclust", 
         tl.col = "black", tl.srt = 45)
```


```{r}
ggplot(mtcars, aes(disp, wt)) +
    geom_point()
```


```{r}
ggplot(mtcars, aes(disp, hp)) +
    geom_point() 
```


```{r}
ggplot(mtcars, aes(mpg, wt)) +
    geom_point()
```


```{r}
ggplot(mtcars, aes(mpg, disp)) +
    geom_point()
```


```{r}
ggplot(mtcars, aes(cyl,mpg)) +
    geom_boxplot() +
    labs(x = "cyl",
         y = "mpg")
```


```{r}
ggplot(mtcars, aes(vs,mpg)) +
    geom_boxplot() +
    labs(x = "vs",
         y = "mpg")
```


```{r}
ggplot(mtcars, aes(am,mpg)) +
    geom_boxplot() +
    labs(x = "am",
         y = "mpg")
```


```{r}
ggplot(mtcars, aes(am,hp)) +
    geom_boxplot() +
    labs(x = "am",
         y = "hp")
```


```{r}
ggplot(mtcars, aes(gear,hp)) +
    geom_boxplot() +
    labs(x = "gear",
         y = "hp")
```


```{r}
ggplot(mtcars, aes(vs,hp)) +
    geom_boxplot() +
    labs(x = "vs",
         y = "hp")
```


```{r}
ggplot(mtcars, aes(cyl,hp)) +
    geom_boxplot() +
    labs(x = "cyl",
         y = "hp")
```

# Bivariate Analysis
## Talk about some of the relationships you observed in this part of the investigation. How did the feature(s) of interest vary with other features in the dataset?
Mpg correlates moderately with rear axle ratio and quarter mile time. As the rear axle ratio and quarter mile time increase the variance in the fuel consumption increases. Mpg had a strong negative correlation with displacement and weight of the car. 


## Did you observe any interesting relationships between the other features (not the main feature(s) of interest)?
The displacement of the car tend to correlate with the horsepower of the car. The higher the displacement of the car the more the horsepower the car will have.


## What was the strongest relationship you found?
The displacement of the car and weight of the car have a strong and positive correlation with each other. The mpg of the car also correlated with the rear axle ratio of the car but not strongly. 


# Multivariate Plots Section
```{r}
ggplot(mtcars, 
       aes(x = mpg, 
           fill = vs)) +
  geom_density(alpha = 0.4)
```


```{r}
ggplot(mtcars, 
       aes(x = mpg, 
           fill = am)) +
  geom_density(alpha = 0.4)
```


```{r}
ggplot(mtcars, 
       aes(x = mpg, 
           fill = gear)) +
  geom_density(alpha = 0.4)
```


```{r}
ggplot(mtcars, 
       aes(x = mpg, 
           fill = cyl)) +
  geom_density(alpha = 0.4)
```


# Multivariate Analysis
## Talk about some of the relationships you observed in this part of the investigation.
Cars with automatic transmission had low fuel consumption compared to manual transmission. But cars with manual transmission had a wide distribution of fuel consumption.

## Were there any interesting or surprising interactions between features?
Car with engines that have 8 cylinders had low fuel consumption compared to engines that have 4 cylinders. This was interesting as cars with 8 cylinders are assumed to consume alot fuel. 


# Final Plots and Summary
## Plot One
```{r, message=FALSE, warning=FALSE}
ggplot(mtcars, aes(x = mpg)) +
    geom_histogram() +
   labs(title = "Distribution of Fuel consumption",
        x = "mpg",
        y = "Number of cars") + 
   theme_minimal()
```

## Description One
The distribution of fuel consumption of the cars seem to skew to the right. Most cars have a fuel consumption of below 25 mpg.

## Plot Two
```{r}
ggplot(mtcars, aes(cyl,mpg)) +
    geom_boxplot() +
    labs(title = "Fuel Consumption by Cylinders",
         x = "cyl",
         y = "mpg") +
   theme_minimal()
```
## Description Two
Cars with 4 cylinders in their engines have a high fuel consumption. Some cars with 8 cylinders had a fuel consumption of 10 which was seen as an outlier.


## Plot Three
```{r}
ggplot(mtcars, 
       aes(x = mpg, 
           fill = gear)) +
  geom_density(alpha = 0.4) +
   labs(title = "Fuel Consumption by Gears",
         x = "mpg") +
   theme_minimal()
```
## Description Three
Cars with 4 and 5 forward gears had high fuel consumption compared to cars with 3 forward gears which had low fuel consumption. Cars with  5 forward gears had a wide distribution of fuel consumption compared to cars with 3 and 4 forward gears.


# Reflection

The mtcars dataset contains information about 32 cars. The dataset comprise of information about fuel consumption and design and performance of the 32 cars.I did some exploration to understand the variables in the dataset. I explored relationship between the fuel consumption of cars with other variables.

There was a good relationship between the fuel consumption of a car with their rear axle ratio. A moderate relationship was also observed between fuel consumption (mpg) and quarter mile time (qsec). The surprising thing was that mpg has negative correlation with features like disp, hp, and wt which i assumed would have positive correlation the fuel consumption of a car.

The limitation of this dataset is it had 32 observations only. The dataset did not also include other cars from other car manufacturers. Give the dataset contains data from 1974 the analysis of fuel consumption of the cars would not reflect on the cars from this century since there have been technological advancement in car manufacturing.



